package com.kluniv.tank;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

/**
 * @author 111
 *
 */
public class Tank {
	private static final int SPEED = 3;
	protected static final int WIDTH = ResourceMgr.goodTankD.getWidth();
	protected static final int HEIGHT = ResourceMgr.goodTankD.getHeight();

	private int x;
	private int y;
	private Dir dir;

	private TankFrame tf = null;

	private boolean moving = true;
	private boolean living = true;

	private Group group = Group.BAD;
	private Random random = new Random();
	protected Rectangle rect = new Rectangle();

	public Tank(int x, int y, Dir dir, Group group, TankFrame tf) {
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.tf = tf;
		this.group = group;

		rect.x = this.x;
		rect.y = this.y;
		rect.width = Tank.WIDTH;
		rect.height = Tank.HEIGHT;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public Dir getDir() {
		return dir;
	}

	public void setDir(Dir dir) {
		this.dir = dir;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public boolean isMoving() {
		return moving;
	}

	public void setMoving(boolean moving) {
		this.moving = moving;
	}

	public void paint(Graphics g) {
		if (!living)
			tf.tanks.remove(this);

		switch (dir) {
		case LEFT:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null);
			break;
		case UP:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null);
			break;
		case RIGHT:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null);
			break;
		case DOWN:
			g.drawImage(this.group == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null);
			break;
		}
		move();
	}

	private void move() {
		if (!moving)
			return;

		switch (dir) {
		case UP:
			y -= SPEED;
			break;
		case DOWN:
			y += SPEED;
			break;
		case LEFT:
			x -= SPEED;
			break;
		case RIGHT:
			x += SPEED;
			break;
		default:
			break;
		}

		if (this.group == Group.BAD && random.nextInt(100) > 90)
			this.fire();
		if (this.group == Group.BAD && random.nextInt(100) > 95)
			randomDir();

		boundsCheck();

		rect.x = this.x;
		rect.y = this.y;
	}

	private void boundsCheck() {
		if (this.x < 2)
			x = 2;
		if (this.y < 28)
			y = 28;
		if (this.x > TankFrame.GAME_WIDTH - Tank.WIDTH - 2)
			x = TankFrame.GAME_WIDTH - Tank.WIDTH - 2;
		if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2)
			y = TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2;
	}

	private void randomDir() {
		dir = Dir.values()[random.nextInt(4)];
	}

	public void fire() {
		int bx = x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
		int by = y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
		tf.bullets.add(new Bullet(bx, by, this.dir, this.group, this.tf));

		// if (this.group == Group.GOOD)
		// new Thread(() -> new Audio("audio/tank_fire.wav").play()).start();
	}

	public void die() {
		living = false;
	}

}
